package top.lixunda.books3.controller;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import top.lixunda.books3.entity.User;
import top.lixunda.books3.model.Message;
import top.lixunda.books3.model.SimpleBook;
import top.lixunda.books3.service.RecommendService;
import top.lixunda.books3.service.impl.RecommendServiceImpl;
import top.lixunda.books3.util.Log;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/userBookTag")
public class RecommendController {

    private final RecommendService recommendService;

    @Autowired
    public RecommendController(RecommendService userBookTagService) {
        this.recommendService = userBookTagService;
    }

    @RequestMapping(value = "/userBook")
    @ResponseBody
    public Message querySimpleBook(@SessionAttribute(value = "user", required = false) User user, long page){
        Map<String, Object> map = recommendService.querySimpleBookAndCountByUserTag(user, page);
        //1. 根据用户获取推荐书籍列表
        List<SimpleBook> simpleBookList = (List<SimpleBook>) map.get("simpleBook");
        //2. 获取书籍数量
        long count = (long) map.get("count");
        Log.d("-------------->  UserBookTag  "+new Gson().toJson(map));
        return Message.successData(simpleBookList).setTotal(count).setCount(simpleBookList.size())
                .setFirst(RecommendServiceImpl.PAGE_COUNT*page);
    }

    @RequestMapping(value = "/userBookIndex")
    @ResponseBody
    public Message querySimpleBook(@SessionAttribute(value = "user",required = false) User user, long start, long count){
        Map<String, Object> map = recommendService.querySimpleBookAndCountByUserTag(user, start, count);
        //1. 根据用户获取推荐书籍列表
        List<SimpleBook> simpleBookList = (List<SimpleBook>) map.get("simpleBook");
        //2. 获取书籍数量
        long _count = (long) map.get("count");
        Log.d("-------------->  UserBookTag  "+new Gson().toJson(map));
        return Message.successData(simpleBookList).setTotal(_count).setCount(simpleBookList.size())
                .setFirst(start);
    }

}
